package com.borya.action.mobile;

import com.alibaba.fastjson.JSONObject;
import com.borya.action.BaseAction;
import com.borya.framework.annotation.Action;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.JsonResult;
import com.borya.framework.annotation.UrlMapping;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.core.ServerResponse;
import com.borya.model.db.MerchantInfoFill;
import com.borya.model.db.User;
import com.borya.service.mobile.MerchantFillService;
import com.borya.statuscode.StatusCode;
import org.apache.log4j.Logger;

import java.util.Objects;


/**
 * 商户实名补录、补充
 * @author Administrator
 *
 */
@UrlMapping("/c/merchantFill/")
@Action
public class MerchantFillAction extends BaseAction {
	
	
	private Logger log = Logger.getLogger(getClass());


	@Autowired("merchantFillService")
	private MerchantFillService merchantFillService;
	
	/**
	 * 获取商户实名补录状态
	 * @param request
	 * @param response
	 * @return
	 */
	@UrlMapping("getStatus")
	@JsonResult
	public String getStatus(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		String result = merchantFillService.getStatus(user);
		log.info("OUT[" + result + "]");
		return result;
	}
	
	
	@UrlMapping("getInfo")
	@JsonResult
	public String getInfo(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		String result = merchantFillService.getInfo(user);
		log.info("OUT[" + result + "]");
		return result;
	}
	
//	@UrlMapping("upload")
//	@JsonResult
//	public String upload(ServerRequest request, ServerResponse response) {
//		JSONObject json = getJson(request);
//		User user = getUser(request);
//		String result = merchantFillService.upload(user,json);
//		log.info("OUT[" + result + "]");
//		return result;
//	}
	
	@UrlMapping("add")
	@JsonResult
	public String add(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		if(json == null){
			log.warn("请求参数错误");
			return StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("RECV :" + json);
		MerchantInfoFill infoFill = JSONObject.parseObject(json.toJSONString(), MerchantInfoFill.class);
		if(infoFill == null){
			log.warn("请求参数错误"+json);
			return StatusCode.PARAM_ERROR.toJSON();
		}
		if(infoFill.getSignPicture() == null || infoFill.getSignPicture().length() == 0){
			log.warn("signPicture参数错误"+json);
			return StatusCode.PARAM_ERROR.toJSON();
		}
		String result = merchantFillService.add(user,infoFill);
		log.info("OUT :" + result);
		return result;
	}

    /**
     * 获取商户信息补充内容
     * @param request
     * @param response
     * @return
     */
    @UrlMapping("getReplenish")
    @JsonResult
    public String getReplenish(ServerRequest request, ServerResponse response) {
        User user = getUser(request);
        JSONObject json = getJson(request);
        String result = merchantFillService.getReplenish(user, json);
        log.info("OUT[" + result + "]");
        return result;
    }

    /**
     * 个人商户信息补充(提交)
     *  
     */
    @UrlMapping("addReplenish")
    @JsonResult
    public String addReplenish(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        User user = getUser(request);
        if (Objects.isNull(json)) {
            log.warn("请求参数为空");
            return StatusCode.PARAM_ERROR.toJSON();
        }
        log.info("RECV :" + json);
        MerchantInfoFill infoFill = JSONObject.parseObject(json.toJSONString(), MerchantInfoFill.class);
        if (Objects.isNull(infoFill)) {
            log.warn("请求参数错误" + json);
            return StatusCode.PARAM_ERROR.toJSON();
        }
        String result = merchantFillService.addReplenish(user, infoFill);
        log.info("OUT :" + result);
        return result;
    }
	
}
